<template>
  <div class="preview-mode">
    <!-- 预览工具栏 -->
    <div class="preview-toolbar">
      <div class="page-navigation">
        <el-button 
          @click="$emit('prev-page')" 
          :disabled="currentPageIndex === 0"
          icon="el-icon-arrow-left"
          size="small"
          type="primary"
        >上一页</el-button>
        <span class="page-info">{{ currentPageIndex + 1 }} / {{ pages.length }}</span>
        <el-button 
          @click="$emit('next-page')" 
          :disabled="currentPageIndex === pages.length - 1"
          icon="el-icon-arrow-right"
          size="small"
          type="primary"
        >下一页</el-button>
      </div>
      <div class="toolbar-actions">
        <el-button 
          @click="toggleFullscreen"
          :icon="isFullscreen ? 'el-icon-copy-document' : 'el-icon-full-screen'"
          size="small"
        >{{ isFullscreen ? '退出全屏' : '全屏' }}</el-button>
        <el-button 
          @click="exportCurrentPage"
          icon="el-icon-download"
          size="small"
        >导出当前页</el-button>
      </div>
    </div>

    <!-- 预览画布 -->
    <div class="preview-canvas-wrapper" ref="previewWrapper">
      <div 
        class="preview-canvas" 
        :style="previewCanvasStyle"
        ref="previewCanvas"
      >
        <div
          v-for="component in currentPage.components"
          :key="component.id"
          class="preview-component"
          :style="getComponentStyle(component)"
        >
          <!-- 预览模式下的组件渲染 -->
          <div v-if="component.type === 'text'" class="text-component" v-html="component.content"></div>
          
          <div v-else-if="component.type === 'image'" class="image-component">
            <img 
              v-if="component.src" 
              :src="component.src" 
              alt="图片" 
              :style="{ objectFit: component.objectFit || 'contain' }"
            />
          </div>
          
          <div v-else-if="component.type === 'map'" class="map-component">
            <div class="map-placeholder">
              <i class="el-icon-location"></i>
              <p>地图区域 ({{ component.mapType || '标准' }})</p>
              <p class="zoom-info">缩放级别: {{ component.zoomLevel || 10 }}</p>
            </div>
          </div>
          
          <div v-else-if="component.type === 'compass'" class="compass-component">
            <div 
              class="compass" 
              :style="{ 
                transform: `rotate(${component.rotation || 0}deg)`,
                borderColor: component.circleColor || '#333333'
              }"
            >
              <div 
                class="compass-needle" 
                :style="{ backgroundColor: component.needleColor || '#ff4444' }"
              ></div>
              <div class="compass-label">N</div>
            </div>
          </div>
          
          <div v-else-if="component.type === 'scale'" class="scale-component">
            <div 
              class="scale-bar" 
              :style="{ 
                borderColor: component.scaleColor || '#333333',
                color: component.scaleColor || '#333333'
              }"
            >
              <div class="scale-segment"></div>
              <div class="scale-segment"></div>
              <div class="scale-segment"></div>
            </div>
            <div 
              class="scale-text"
              :style="{ color: component.scaleColor || '#333333' }"
            >{{ component.scaleText || '0 100 200 300m' }}</div>
          </div>
          
          <div v-else-if="component.type === 'rectangle'" class="shape-component rectangle">
            <div v-if="component.text" class="shape-text" v-html="component.text"></div>
          </div>
          
          <div v-else-if="component.type === 'circle'" class="shape-component circle">
            <div v-if="component.text" class="shape-text" v-html="component.text"></div>
          </div>
          
          <div v-else-if="component.type === 'triangle'" class="shape-component triangle">
            <div v-if="component.text" class="shape-text" v-html="component.text"></div>
          </div>
        </div>
      </div>
    </div>

    <!-- 页面缩略图导航 -->
    <div class="thumbnail-navigation" v-if="pages.length > 1">
      <div class="thumbnails-container">
        <div 
          class="thumbnail-item"
          v-for="(page, index) in pages"
          :key="page.id"
          :class="{ active: index === currentPageIndex }"
          @click="$emit('switch-page', index)"
        >
          <div class="thumbnail-canvas">
            <div class="thumbnail-number">{{ index + 1 }}</div>
            <div class="thumbnail-preview">
              <!-- 简化的缩略图预览 -->
              <div 
                v-for="component in page.components.slice(0, 5)"
                :key="component.id"
                class="thumbnail-component"
                :style="getThumbnailStyle(component)"
              ></div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  name: 'PreviewMode',
  props: {
    pages: {
      type: Array,
      required: true
    },
    currentPageIndex: {
      type: Number,
      required: true
    },
    canvasStyle: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      isFullscreen: false,
      scale: 1
    }
  },
  computed: {
    currentPage() {
      return this.pages[this.currentPageIndex] || this.pages[0]
    },
    previewCanvasStyle() {
      const baseStyle = { ...this.canvasStyle }
      
      if (this.isFullscreen) {
        // 全屏模式下自适应缩放
        return {
          ...baseStyle,
          transform: `scale(${this.scale})`,
          transformOrigin: 'center center'
        }
      }
      
      return baseStyle
    }
  },
  mounted() {
    this.calculateScale()
    window.addEventListener('resize', this.calculateScale)
    document.addEventListener('keydown', this.handleKeyDown)
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.calculateScale)
    document.removeEventListener('keydown', this.handleKeyDown)
  },
  methods: {
    getComponentStyle(component) {
      const style = {
        position: 'absolute',
        left: component.x + 'px',
        top: component.y + 'px',
        width: component.width + 'px',
        height: component.height + 'px',
        opacity: component.opacity || 1
      }

      // 设置背景和边框样式
      if (!['text', 'compass', 'scale'].includes(component.type)) {
        if (component.backgroundColor && component.backgroundColor !== 'transparent') {
          style.backgroundColor = component.backgroundColor
        }
        if (component.borderColor && component.borderWidth > 0) {
          style.border = `${component.borderWidth}px solid ${component.borderColor}`
        }
      }

      // 文本样式
      if (component.type === 'text' || component.text !== undefined) {
        style.fontSize = (component.fontSize || 14) + 'px'
        style.color = component.textColor || '#333333'
        style.textAlign = component.textAlign || 'center'
        style.fontWeight = component.fontWeight || 'normal'
      }

      // 三角形特殊处理
      if (component.type === 'triangle') {
        style.background = 'transparent'
        style['--bg-color'] = component.backgroundColor || '#FF9800'
      }

      return style
    },
    getThumbnailStyle(component) {
      const scaleX = 60 / parseInt(this.canvasStyle.width)
      const scaleY = 40 / parseInt(this.canvasStyle.height)
      
      return {
        position: 'absolute',
        left: (component.x * scaleX) + 'px',
        top: (component.y * scaleY) + 'px',
        width: (component.width * scaleX) + 'px',
        height: (component.height * scaleY) + 'px',
        backgroundColor: component.backgroundColor || '#ccc',
        opacity: 0.8
      }
    },
    toggleFullscreen() {
      this.isFullscreen = !this.isFullscreen
      this.$nextTick(() => {
        this.calculateScale()
      })
    },
    calculateScale() {
      if (!this.isFullscreen) {
        this.scale = 1
        return
      }
      
      const wrapper = this.$refs.previewWrapper
      if (!wrapper) return
      
      const wrapperRect = wrapper.getBoundingClientRect()
      const canvasWidth = parseInt(this.canvasStyle.width)
      const canvasHeight = parseInt(this.canvasStyle.height)
      
      const scaleX = (wrapperRect.width - 40) / canvasWidth
      const scaleY = (wrapperRect.height - 40) / canvasHeight
      
      this.scale = Math.min(scaleX, scaleY, 1)
    },
    handleKeyDown(event) {
      if (event.key === 'ArrowLeft' && this.currentPageIndex > 0) {
        this.$emit('prev-page')
      } else if (event.key === 'ArrowRight' && this.currentPageIndex < this.pages.length - 1) {
        this.$emit('next-page')
      } else if (event.key === 'Escape' && this.isFullscreen) {
        this.toggleFullscreen()
      }
    },
    exportCurrentPage() {
      // 创建canvas元素用于导出
      const canvas = document.createElement('canvas')
      const ctx = canvas.getContext('2d')
      
      canvas.width = parseInt(this.canvasStyle.width)
      canvas.height = parseInt(this.canvasStyle.height)
      
      // 设置背景
      ctx.fillStyle = this.canvasStyle.backgroundColor || '#ffffff'
      ctx.fillRect(0, 0, canvas.width, canvas.height)
      
      // 简化的导出逻辑 - 这里可以进一步完善
      const link = document.createElement('a')
      link.download = `页面${this.currentPageIndex + 1}.png`
      link.href = canvas.toDataURL()
      link.click()
      
      this.$message.success('页面导出成功')
    }
  }
}
</script>

<style scoped>
.preview-mode {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: #000000;
  color: #ffffff;
}

.preview-toolbar {
  height: 60px;
  background: rgba(0,0,0,0.9);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  border-bottom: 1px solid rgba(255,255,255,0.1);
}

.page-navigation {
  display: flex;
  align-items: center;
  gap: 15px;
}

.page-info {
  color: #ffffff;
  font-size: 14px;
  font-weight: 500;
  min-width: 80px;
  text-align: center;
}

.toolbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.preview-canvas-wrapper {
  flex: 1;
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 20px;
  overflow: hidden;
}

.preview-canvas {
  background: #ffffff;
  box-shadow: 0 4px 20px rgba(0,0,0,0.3);
  position: relative;
  transition: transform 0.3s ease;
}

.preview-component {
  user-select: none;
}

/* 组件样式 - 与编辑模式保持一致 */
.text-component {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  word-wrap: break-word;
}

.image-component {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.image-component img {
  max-width: 100%;
  max-height: 100%;
}

.map-component {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border: 1px solid #d0d0d0;
  background: #f0f8ff;
}

.map-placeholder {
  text-align: center;
  color: #666;
}

.map-placeholder i {
  font-size: 24px;
  display: block;
  margin-bottom: 5px;
}

.zoom-info {
  font-size: 10px;
  color: #999;
  margin-top: 5px;
}

.compass-component {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.compass {
  width: 40px;
  height: 40px;
  border: 2px solid #333;
  border-radius: 50%;
  position: relative;
  background: #ffffff;
  transition: transform 0.3s ease;
}

.compass-needle {
  position: absolute;
  top: 2px;
  left: 50%;
  transform: translateX(-50%);
  width: 2px;
  height: 16px;
  background: #ff4444;
}

.compass-label {
  position: absolute;
  top: -20px;
  left: 50%;
  transform: translateX(-50%);
  font-size: 12px;
  font-weight: bold;
  color: #333;
}

.scale-component {
  width: 100%;
  height: 100%;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: flex-start;
  padding: 5px;
}

.scale-bar {
  display: flex;
  height: 8px;
  border-bottom: 2px solid #333;
  border-left: 2px solid #333;
  border-right: 2px solid #333;
  width: 90%;
}

.scale-segment {
  flex: 1;
  border-right: 1px solid #333;
  background: #333;
}

.scale-segment:nth-child(even) {
  background: #ffffff;
}

.scale-segment:last-child {
  border-right: none;
}

.scale-text {
  font-size: 10px;
  margin-top: 2px;
  color: #333;
}

.shape-component {
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.shape-component.rectangle {
  border-radius: 4px;
}

.shape-component.circle {
  border-radius: 50%;
}

.shape-component.triangle {
  background: transparent !important;
}

.shape-component.triangle::before {
  content: '';
  position: absolute;
  top: 0;
  left: 50%;
  transform: translateX(-50%);
  width: 0;
  height: 0;
  border-left: 50px solid transparent;
  border-right: 50px solid transparent;
  border-bottom: 100px solid var(--bg-color, #FF9800);
}

.shape-text {
  position: relative;
  z-index: 1;
  color: #ffffff;
  font-weight: bold;
  text-align: center;
  word-wrap: break-word;
  max-width: 90%;
  text-shadow: 1px 1px 2px rgba(0,0,0,0.5);
}

/* 缩略图导航 */
.thumbnail-navigation {
  height: 80px;
  background: rgba(0,0,0,0.9);
  border-top: 1px solid rgba(255,255,255,0.1);
  display: flex;
  align-items: center;
  padding: 0 20px;
}

.thumbnails-container {
  display: flex;
  gap: 10px;
  overflow-x: auto;
}

.thumbnail-item {
  cursor: pointer;
  transition: all 0.2s;
  opacity: 0.6;
}

.thumbnail-item.active {
  opacity: 1;
  transform: scale(1.1);
}

.thumbnail-canvas {
  width: 80px;
  height: 60px;
  background: #ffffff;
  border: 2px solid transparent;
  border-radius: 4px;
  position: relative;
  overflow: hidden;
}

.thumbnail-item.active .thumbnail-canvas {
  border-color: #409EFF;
}

.thumbnail-number {
  position: absolute;
  top: 2px;
  left: 2px;
  background: rgba(0,0,0,0.7);
  color: #ffffff;
  font-size: 10px;
  padding: 1px 4px;
  border-radius: 2px;
  z-index: 2;
}

.thumbnail-preview {
  width: 100%;
  height: 100%;
  position: relative;
}

.thumbnail-component {
  border-radius: 1px;
}

/* 全屏模式样式 */
.preview-mode.fullscreen {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 9999;
}

/* 滚动条样式 */
.thumbnails-container::-webkit-scrollbar {
  height: 4px;
}

.thumbnails-container::-webkit-scrollbar-track {
  background: rgba(255,255,255,0.1);
}

.thumbnails-container::-webkit-scrollbar-thumb {
  background: rgba(255,255,255,0.3);
  border-radius: 2px;
}

.thumbnails-container::-webkit-scrollbar-thumb:hover {
  background: rgba(255,255,255,0.5);
}
</style>
